package com.hotel.common.user.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotel.common.param.UserVipAdd;
import com.hotel.common.user.entity.Vip;
import com.hotel.common.user.entity.VipOrder;
import com.hotel.common.user.mapper.VipMapper;
import com.hotel.common.user.mapper.VipOrderMapper;
import com.hotel.common.user.service.VipOrderService;
import com.hotel.core.alipay.AliPayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 开通vip订单表 服务实现类
 * </p>
 *
 * @author KK
 * @since 2024-06-15
 */
@Service
public class VipOrderServiceImpl extends ServiceImpl<VipOrderMapper, VipOrder> implements VipOrderService {

    @Autowired
    private VipMapper vipMapper;
    @Autowired
    private VipOrderMapper vipOrderMapper;

    @Override
    public VipOrder createOrder(UserVipAdd userVipAdd) {
        VipOrder vipOrder = new VipOrder();
        /**
         * 创建查询Wrapper，用于根据用户ID查询VIP信息。
         * 使用QueryWrapper的eq方法指定查询条件为"user_id"等于userVipAdd.getUserId()。
         * 这里的目的是为了精确查询特定用户ID对应的VIP信息，以便后续的操作。
         */
        QueryWrapper<Vip> userId = (new QueryWrapper<Vip>()).eq("user_id", userVipAdd.getUserId());


        Vip vip = vipMapper.selectOne(userId);
        //查看当前用户是否有vip
        if (vip == null) {
            vipOrder.setType(1);
        } else {
            vipOrder.setType(2);
        }
        vipOrder.setUserId(userVipAdd.getUserId());
        vipOrder.setPayType(userVipAdd.getPayType());
        vipOrder.setMoney(userVipAdd.getMoney());
        vipOrder.setVipType(userVipAdd.getVipType());
        String no = "v" + System.currentTimeMillis() + RandomUtil.randomNumbers(6);
        vipOrder.setNo(no);
        vipOrder.setStatus(1);//先设置成未支付
        vipOrder.setCtime(new Date());
        int i = vipOrderMapper.insert(vipOrder);
        if (i>0){
            String html = AliPayUtil.createPay(no, userVipAdd.getMoney(), "购买会员");
            return vipOrder;
        }
        return vipOrder;

    }
}
